package org.binglan.youziyuepai.controller.base;

import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.binglan.youziyuepai.enums.ResponseEnum;
import org.binglan.youziyuepai.exception.MyException;
import org.binglan.youziyuepai.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/4/8 12:39
 */
public class BasicController {


    /**
     * 判断用户是否进行登陆，返回用户id
     *
     * @return
     */
    protected String getUserIdByAuth() {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            throw new MyException(ResponseEnum.AUTHENTICATEERROR.getMessage());
        }
        String jwtToken = (String) subject.getPrincipal();
        return  JwtUtils.getUsername(jwtToken);

    }

    /**
     * 判断用户是否进行登陆，返回 Subject
     *
     * @return
     */
    protected Subject getSubjectByAuth() {
        // 判断用户是否进行认证
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            throw new MyException(ResponseEnum.AUTHENTICATEERROR.getMessage());
        }
        return subject;
    }
}
